package cn.itcast.n4;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * className: UnsafeAccessor
 * description:
 * date: 2023/2/27-17:29
 * <p>
 * project: concurrent
 * package: cn.itcast.n4
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
public class UnsafeAccessor {
    private static final Unsafe UNSAFE;

    static {
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            UNSAFE = (Unsafe) theUnsafe.get(null);
        } catch (NoSuchFieldException | IllegalAccessException exception) {
            throw new Error(exception);
        }
    }

    public static Unsafe getUnsafe() {
        return UNSAFE;
    }
}
